home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0028_File Manager Drag-Drop.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  1.9 KB  |  62 lines

  1.  
  2. To use File Manager Drag & Drop, add a method to your form that
  3. handles the WM_DROPFILES message.  For example, the following
  4. would be placed in the TForm1 declaration in the protected
  5. section:
  6.  
  7.     ...
  8.     procedure WMDropFiles(var msg : TMessage); message WM_DROPFILES;
  9.     ...
  10.  
  11. You would typically activate file drag & drop by calling
  12. DragAcceptFiles() in the OnCreate event, and turn it off
  13. with a subsequent call to DragAcceptFiles() in the OnClose
  14. or OnDestroy events.  The code follows:
  15.  
  16. procedure TForm1.WMDropFiles(var msg : TMessage);
  17. var
  18.   i, n  : word;
  19.   size  : word;
  20.   fname : string;
  21.   hdrop : word;
  22. begin
  23.   {1. Get the drop handle.}
  24.   hdrop := msg.WParam;
  25.   {2. Find out how many files were dropped by passing $ffff in arg #2.}
  26.   n := DragQueryFile(hdrop, $ffff, nil, 0);
  27.   {3. Loop through, reading in the filenames (w/full paths).}
  28.   for i := 0 to (n - 1) do begin
  29.     {4. Get the size of the filename string by passing 0 in arg #4.}
  30.     size := DragQueryFile(hdrop, i, nil, 0);
  31.     {5. Make sure it won't overflow our string (255 char. limit)}
  32.     if size < 255 then begin
  33.       fname[0] := Chr(size);
  34.       {6. Get the dropped filename.}
  35.       DragQueryFile(hdrop, i, @fname[1], size + 1);
  36.       {-- Do whatever you want to do with fname. --}
  37.     end;
  38.   end;
  39.   {7. Return zero.}
  40.   msg.Result := 0;
  41.   {8. Let the inherited message handler (if there is one) go at it.}
  42.   inherited;
  43. end;
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. begin
  47.   DragAcceptFiles(Handle, true);
  48. end;
  49.  
  50. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  51. begin
  52.   DragAcceptFiles(Handle, false);
  53. end;
  54.  
  55. Keep in mind that you don't have to put all of this stuff
  56. on a form.  Any windowed control that has an HWnd handle
  57. (descendants of TWinControl) should be able to accept
  58. dropped files.
  59.  
  60. I hope this answers your question.
  61. --Mark Johnson
  62.